//
//  HZ_Login_ViewController.m
//  HZ_Beautiful_Music
//
//  Created by dllo on 16/3/30.
//  Copyright © 2016年 ui. All rights reserved.
//

#import "HZ_Login_ViewController.h"
#import "HZCollectViewController.h"
#import "HZJDownLoadViewController.h"
#import "HZJAreadyDownViewController.h"
#import "AppDelegate.h"

#import "HZDayNightTableCell.h"
#import "HZClearCacheTableCell.h"

#import "CleanCaches.h"

@interface HZ_Login_ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSString *string;

@end

@implementation HZ_Login_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor orangeColor];
    
    UIImageView *imageview = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageview.image = [UIImage imageNamed:@"leftbackiamge"];
    [self.view addSubview:imageview];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    [_tableView registerClass:[HZDayNightTableCell class] forCellReuseIdentifier:@"12"];
    [_tableView registerClass:[HZClearCacheTableCell class] forCellReuseIdentifier:@"clear"];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 150)];
    view.backgroundColor = [UIColor clearColor];
    _tableView.tableHeaderView = view;
    
    [self.view addSubview:_tableView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
        cell.textLabel.text = @"我的最爱";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font = [UIFont systemFontOfSize:18.0f];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor whiteColor];
        return cell;
    }
    if (indexPath.row == 1) {
        HZDayNightTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"12"];
        //    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.titleLable.text = @"夜间模式";
        cell.titleLable.font = [UIFont systemFontOfSize:18.0f];
        cell.backgroundColor = [UIColor clearColor];
        cell.titleLable.textColor = [UIColor whiteColor];
        return cell;
    }
    
    if (indexPath.row == 2) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
        cell.textLabel.text = @"已下载的歌曲";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font = [UIFont systemFontOfSize:18.0f];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor whiteColor];
        return cell;
    }
    
    HZClearCacheTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"clear"];
    //    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.titleLable.text = @"清除缓存";
    cell.titleLable.font = [UIFont systemFontOfSize:18.0f];
    cell.backgroundColor = [UIColor clearColor];
    cell.titleLable.textColor = [UIColor whiteColor];
    
    //    缓存大小
    self.string = [CleanCaches folderSizeAtPath];
    cell.lable.text = _string;
    cell.lable.font = [UIFont systemFontOfSize:17.0f];
    cell.lable.textColor = [UIColor whiteColor];
    return cell;
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        
        HZCollectViewController *collectVC = [[HZCollectViewController alloc] init];
        [tempAppDelegate.leftSlideVC closeLeftView];
        [tempAppDelegate.mainNavigationController pushViewController:collectVC animated:YES];
    }
    
    if (indexPath.row == 3) {
        [self cleanAction];
    }
    if (indexPath.row == 2) {
         AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        HZJAreadyDownViewController *downVC = [[HZJAreadyDownViewController alloc] init];
        [tempAppDelegate.leftSlideVC closeLeftView];
        [tempAppDelegate.mainNavigationController pushViewController:downVC animated:YES];
        
    }
    [_tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

// 清空文件夹方法
-(void)cleanAction
{
    //获取完整路径
    NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
    NSString *cachePath = [path stringByAppendingPathComponent:@"caches"];
    //    NSLog(@"%@", cachePath);
    
    NSFileManager *manage = [NSFileManager defaultManager];
    if ([manage fileExistsAtPath:cachePath]) {
        NSArray *fileNameArray = [manage subpathsAtPath:cachePath];
        for (NSString *fileName in fileNameArray) {
            //拼接绝对路径
            NSString *absolutePath = [cachePath stringByAppendingPathComponent:fileName];
            //通过文件管理者删除文件
            [manage removeItemAtPath:absolutePath error:nil];
        }
    }
    //及时更新label上文件的大小
    self.string = [NSString stringWithFormat:@"%.2fM", [self folderSizeAtPath:cachePath]];
    //    重新加载 告诉tableView缓存值已经改变
    [_tableView reloadData];
}

#warning 计算单个文件大小的方法
-(long long)fileSizeAtPath:(NSString *)path
{
    //创建一个文件管理者
    NSFileManager *manage = [NSFileManager defaultManager];
    if ([manage fileExistsAtPath:path]) {
        return [[manage attributesOfItemAtPath:path error:nil] fileSize];
    }
    return 0;
}

#warning 计算一个文件夹大小
-(float)folderSizeAtPath:(NSString *)path
{
    //创建文件管理者
    NSFileManager *manage = [NSFileManager defaultManager];
    if (![manage fileExistsAtPath:path]) {
        return 0;
    }
    //根据路径获取文件夹里面的元素集合
    //获取集合类型的枚举器
    NSEnumerator *enumrator = [[manage subpathsAtPath:path] objectEnumerator];
    //每次遍历得到的文件名
    NSString *fileName = [NSString string];
    //文件夹大小
    float folderSize = 0;
    while ((fileName = [enumrator nextObject]) != nil) {
        NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:absolutePath];
    }
    return folderSize / (1024.0 * 1024.0);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
